package com.medata.conf;

import com.medata.dataObject.Actiondef;
import com.medata.exeptions.YunExceptions;
import com.medata.service.*;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.File;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * @author ：zb
 * @date ：Created in 2019/5/21 0021 16:18
 * @description：
 */
@Component
public class ScheduledConf {
    @Autowired
    private ResserverService resserverService;
    @Value("${solrPath}")
    private String solrPath;
    @Autowired
    private ActiondefService actiondefService;
    @Autowired
    private LogdetailService logdetailService;
    @Autowired
    private LogSearchService logSearchService;
    @Autowired
    private LogloginService logloginService;
    @Autowired
    private LogMakeDocumentService logMakeDocumentService;
    @Autowired
    private LogdispprjService logdispprjService;

    //每天清楚缓存
    @Scheduled(cron = "0 0 23 * * ?")
    public void deleteTemp(){
        String filePath = resserverService.findByServerport("6896").getRespath();
        String path=filePath+"/tempFiles";
        File file=new File(path);
        if (file.exists()){
            try {
                FileUtils.deleteDirectory(file);
            }catch (Exception e){
                throw new YunExceptions(0,"日常删除文件时出错");
            }
        }
    }

    //根据规定时间清记录日志
    @Scheduled(cron = "00 00 23 * * ?")
    public void deleteLogs(){
        List<Actiondef> all = actiondefService.findAll();
        for (Actiondef actiondef:all){
            Timestamp lastdeletetime = actiondef.getLastdeletetime();
            String lastTime = lastdeletetime.toString();
            DateTimeFormatter formatter=DateTimeFormatter.ofPattern("yyyy-MM-dd");
            LocalDateTime now = LocalDateTime.now(ZoneOffset.of("+8"));
            String NowTime = now.format(formatter);
            lastTime = lastTime.substring(0, 10);
            lastTime=lastTime.replaceAll("-","");
            String newTime=NowTime.replaceAll("-","");
            int i = Integer.valueOf(newTime) - Integer.valueOf(lastTime);
            if (i>=(Integer.valueOf(actiondef.getRoundday()))){
                if (actiondef.getActionid().equals("100")|actiondef.getActionid().equals("101")|actiondef.getActionid().equals("102")|actiondef.getActionid().equals("103")){
                    logMakeDocumentService.deleteByLogtimeBetweenAndActionid(lastdeletetime,Timestamp.valueOf(NowTime+" 00:00:00"),Integer.valueOf(actiondef.getActionid()));
                }else if(actiondef.getActionid().equals("200")|actiondef.getActionid().equals("201")){
                    logloginService.deleteByLogtimeBetweenAndActionid(lastdeletetime,Timestamp.valueOf(NowTime+" 00:00:00"),Integer.valueOf(actiondef.getActionid()));
                }else if(actiondef.getActionid().equals("300")|actiondef.getActionid().equals("301")){
                    logSearchService.deleteByLogTimeBetweenAndActionId(lastdeletetime,Timestamp.valueOf(NowTime+" 00:00:00"),Integer.valueOf(actiondef.getActionid()));
                }else if(actiondef.getActionid().equals("400")){
                    logdetailService.deleteByLogtimeBetween(lastdeletetime,Timestamp.valueOf(NowTime+" 00:00:00"));
                } else if (actiondef.getActionid().equals("500")) {
                    logdispprjService.deleteByLogtimeBetweenAndActionid(lastdeletetime,Timestamp.valueOf(NowTime+" 00:00:00"),actiondef.getActionid());
                }
                actiondef.setLastdeletetime(Timestamp.valueOf(NowTime+" 00:00:00"));
                actiondefService.saveOrUpdate(actiondef);
            }
        }
    }

    public static void main(String[] args) {

    }
}
